﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BookStore.Models;
using System.Data;
namespace BookStore.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            BookModel tbBook = new BookModel();
            FavoriteModel tbFavorite = new FavoriteModel();
            DataTable result = new DataTable();
            DataTable favorite = new DataTable();
            //get top 3 feature book
            try
            {
                favorite = tbFavorite.getTopFeature();
                if (favorite != null)
                {
                    string[][] feature = new string[favorite.Rows.Count][];
                    string[] temp = new string[12];
                    int j = 0;
                    foreach (DataRow row in favorite.Rows)
                    {
                        try
                        {
                            temp = tbBook.getBookbyID(Convert.ToInt32(row["book_id"]));
                        }
                        catch (SystemException ex)
                        {
                        }
                        finally
                        {
                            feature[j] = temp;
                            j++;
                        }
                    }
                    ViewData["feature"] = feature;
                }
                else
                {
                    ViewData["feature"] = null;
                }
            }
            catch (SystemException ex)
            {
                ViewData["feature"] = null;
            }
            //get top 10 new book
            result = new DataTable();
            result = tbBook.getNewBook();
            if (result != null)
            {
                try
                {
                    string[][] news = new string[result.Rows.Count][];
                    int i = 0;
                    foreach (DataRow row in result.Rows)
                    {
                        news[i] = new string[3];
                        news[i][0] = row["book_name"].ToString();
                        news[i][1] = row["image_url"].ToString();
                        if (news[i][1] == "" || news[i][1] == null)
                            news[i][1] = "/content/images/no_image.jpg";
                        news[i][2] = row["book_id"].ToString();
                        i++;
                    }

                    ViewData["news"] = news;

                }
                catch (SystemException ex)
                {
                    
                }
            }
            else
            {
                ViewData["news"] = null;
            }

            return View();
        }

        public ActionResult About()
        {
            return View();
        }
        public ActionResult Terms()
        {
            return View();
        }
    }
}
